// src/store/modules/user.js
const state = {
	token: '121212121212212',
	userInfo: null,
	globalInfo: 'testInfo',
}

const mutations = {
	SET_TOKEN(state, token) {
		state.token = token
	},
	SET_USER_INFO(state, info) {
		state.userInfo = info
	}
}

const actions = {
	// 异步获取用户信息（示例：调用微信登录接口）
	login({
		commit
	}, code) {
		return new Promise((resolve, reject) => {
			// 调用微信小程序登录 API
			wx.login({
				success: (res) => {
					// 假设通过 code 获取 token 和用户信息
					commit('SET_TOKEN', 'xxx-token')
					commit('SET_USER_INFO', {
						name: 'test'
					})
					resolve()
				},
				fail: reject
			})
		})
	}
}

const getters = {
	token: state => !!state.token,
	globalInfo: state => state.globalInfo
}

export default {
	namespaced: true, // 启用命名空间，避免模块间命名冲突
	state,
	mutations,
	actions,
	getters
}